package com.wanchuang.qhbb.module.allow;

/**
 * 文件名 : EaringDetailFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:47
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.adapter.AllowApplyAdapter;
import com.wanchuang.qhbb.base.BaseFragment;
import com.wanchuang.qhbb.help.UserHelper;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.WebApis;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.proto.AgentPolicy;
import com.wanchuang.qhbb.proto.Mystock;
import com.wanchuang.qhbb.web.ui.WebViewActivity;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AllowApplyFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.fragment_recycleView)
    RecyclerView fragmentRecycleView;


    private int page = 1; // 当前页数
    private int position;  // 当前viewPage的位置 0:提现消息 1:奖励消息 2:担保消息 3:系统消息

    private List<AgentPolicy.modify_policy_log> datas = new ArrayList<>(); // 提现消息
    private AllowApplyAdapter mAdapter; // 提现消息适配器
    private String type;
    private int pagesize = 10;

    public static AllowApplyFragment NewInstance(int instanceType) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        AllowApplyFragment messageTypeFragment = new AllowApplyFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbinder = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        initRecycle();

    }

    /**
     * 加载数据
     */
    public void loadData() {
        type = "0";  // 0 别人调拨给我，1 我调拨给别人 ,2 我拒接了别人调拨, 3 已同意， 4 已拒接
        switch (position) {
            case 0:
            case 3:
                type = "3";
                break;
            case 1:
            case 4:
                type = "4";
                break;
            case 2:
                type = "2";
                break;
            case 5:
                type = "5";
                break;
        }
        Mystock.get_sn_transfer_list.Builder builder = Mystock.get_sn_transfer_list.newBuilder();
        builder.setState(type);
        NetworkManager.INSTANCE.post(Apis.get_modify_logs, builder.build().toByteArray(), page, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {




                List<AgentPolicy.modify_policy_log> lists =
                        AgentPolicy.get_modify_logs.parseFrom(response).getModifyPolicyLogsList();
                mAdapter.addData(lists);
                int size = lists.size();
                if (size < 10) {
                    mAdapter.getLoadMoreModule().loadMoreEnd(true);
                } else {
                    mAdapter.getLoadMoreModule().loadMoreComplete();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });

    }


    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        fragmentRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        fragmentRecycleView.setAdapter(mAdapter = new AllowApplyAdapter(mContext, datas, UserHelper.getUser().getMember_id()));
        // 每个item的点击事件

        switch (position) {
            case 0:
                mAdapter.setEmptyView(R.layout.recycle_empty);
                break;
            case 1:
                mAdapter.setEmptyView(R.layout.recycle_empty);
                break;
            case 2:
                mAdapter.setEmptyView(R.layout.recycle_empty);
                break;
            case 3:
                mAdapter.setEmptyView(R.layout.recycle_empty);
                break;
            case 4:
                mAdapter.setEmptyView(R.layout.recycle_empty);
                break;
        }
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            page = mAdapter.getData().size() / pagesize + 1;
            loadData();
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
          WebViewActivity.startActivity(mContext, WebApis.transferApplication + "?id=" + datas.get(position).getId()+"&type="+this.position);
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        datas.clear();
        mAdapter.setNewData(datas);
        loadData();
    }
}
